﻿using DeHeng_Mes.Model.Table;
using DeHeng_Mes.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeHeng_Mes.Dao
{
    public class ProductionPowerDao
    {
        /// <summary>
        /// 判断当日产量是否保存
        /// </summary>
        /// <param name="name"></param>
        /// <param name="now"></param>
        /// <param name="production"></param>
        /// <returns></returns>
        public bool ProductionIsSave(string name, string date, string production)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<TraceProduction>()
                         .Where(x => x.PlcName == name)
                         .Where(x => x.DateTime == date)
                         .Where(x => x.FProduction == production)
                         .Any();
            }
        }

        /// <summary>
        /// 更新当日产量数据
        /// </summary>
        /// <param name="name"></param>
        /// <param name="date"></param>
        /// <param name="production"></param>
        public void UpdateProduction(string name, string date, string production)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Updateable<TraceProduction>()
                  .SetColumns(x => x.FProduction == production)
                  .Where(x => x.PlcName == name)
                  .Where(x => x.DateTime == date)
                  .ExecuteCommand();
            }
        }

        /// <summary>
        /// 插入数据
        /// </summary>
        /// <param name="name"></param>
        /// <param name="date"></param>
        /// <param name="production"></param>
        public void ProductionSave(string name, string date, string production)
        {
            var traceProduction = new TraceProduction()
            {
                DateTime = date,
                PlcName = name,
                FProduction = production
            };

            using (var db = SqlSugarConfig.Db)
            {
                db.Insertable(traceProduction).ExecuteCommand();
            }
        }

        /// <summary>
        /// 能耗是否保存
        /// </summary>
        /// <param name="name"></param>
        /// <param name="date"></param>
        /// <param name="power"></param>
        /// <returns></returns>
        public bool PowerIsSave(string name, string date, string power)
        {
            using (var db = SqlSugarConfig.Db)
            {
                return db.Queryable<TracePower>()
                         .Where(x => x.PlcName == name)
                         .Where(x => x.DateTime == date)
                         .Where(x => x.Fpower == power)
                         .Any();
            }
        }

        /// <summary>
        /// 更新能耗
        /// </summary>
        /// <param name="name"></param>
        /// <param name="date"></param>
        /// <param name="power"></param>
        public void UpdatePower(string name, string date, string power)
        {
            using (var db = SqlSugarConfig.Db)
            {
                db.Updateable<TracePower>()
                  .SetColumns(x => x.Fpower == power)
                  .Where(x => x.PlcName == name)
                  .Where(x => x.DateTime == date)
                  .ExecuteCommand();
            }
        }

        /// <summary>
        /// 存储能耗数据
        /// </summary>
        /// <param name="name"></param>
        /// <param name="date"></param>
        /// <param name="power"></param>
        public void PowerSave(string name, string date, string power)
        {
            var traceProduction = new TracePower()
            {
                DateTime = date,
                PlcName = name,
                Fpower = power
            };

            using (var db = SqlSugarConfig.Db)
            {
                db.Insertable(traceProduction).ExecuteCommand();
            }
        }
    }
}
